<template>
  <div>
    <el-row :gutter="20" type="flex" justify="center">
      <!-- 项目列表 -->
      <el-col :span="23"
        ><div class="grid-content bg-purple">
          <el-card class="box-card">
            <div class="mask">
              <!-- 搜索与添加区域 -->
              <el-row :gutter="20">
                <el-col :span="8">
                  <div class="searchBox">
                    <el-select
                      v-model="searchType"
                      placeholder="请选择搜索类型"
                      @change="handleSearchSelectInputChange"
                    >
                      <el-option
                        v-for="item in searchTypeOptions"
                        :key="item.value"
                        :label="item.label"
                        :value="item.value"
                      >
                      </el-option>
                    </el-select>
                    <el-input
                      :placeholder="searchTypePlaceholder"
                      v-model="searchInputKey"
                      clearable
                      @input="projectSearchInputChange"
                    >
                    </el-input>
                  </div>
                </el-col>
              </el-row>

              <el-container style="height: 86%" width="100%">
                <el-main>
                  <!-- 用户列表区域 -->
                  <el-table :data="projectList" border stripe>
                    <el-table-column type="index" label="ID"></el-table-column>
                    <el-table-column
                      label="项目编号"
                      prop="no"
                      width="120px"
                    ></el-table-column>
                    <el-table-column label="项目名称" prop="name">
                      <template slot-scope="scope">
                        <div
                          class="projectName"
                          @click="handleProjectDetailShow(scope.row)"
                        >
                          {{ scope.row.name }}
                        </div>
                      </template>
                    </el-table-column>
                    <el-table-column
                      label="主持人"
                      prop="commander"
                    ></el-table-column>
                    <el-table-column
                      label="立项机构"
                      prop="initiation_agency"
                      width="130px"
                    ></el-table-column>
                    <el-table-column
                      label="立项时间"
                      prop="initial_time"
                      width="102px"
                    ></el-table-column>
                    <el-table-column
                      label="学科分类"
                      prop="subject_classification"
                    ></el-table-column>
                    <el-table-column
                      label="项目分类"
                      prop="category"
                    ></el-table-column>
                    <el-table-column
                      label="经费"
                      prop="budget"
                      width="80px"
                    ></el-table-column>
                    <el-table-column label="状态" prop="status" width="66px">
                      <template slot-scope="scope">
                        <span v-if="scope.row.status == 1">已审核</span>
                        <span v-else-if="scope.row.status == 0">未审核</span>
                      </template>
                    </el-table-column>
                  </el-table>
                </el-main>
              </el-container>

              <!-- 分页区域 -->
              <el-pagination
                @size-change="handleSizeChange"
                @current-change="handleCurrentChange"
                :current-page="queryInfo.pagenum"
                :page-sizes="[1, 2, 5, 10]"
                :page-size="queryInfo.pagesize"
                layout="total, sizes, prev, pager, next, jumper"
                :total="total"
              >
              </el-pagination>
            </div>
          </el-card></div
      ></el-col>
    </el-row>
    <myProjectDetail></myProjectDetail>
  </div>
</template>

<script>
import myProjectDetail from "../../components/project/myProjectDetail.vue";
import { mapState } from "vuex";

export default {
  name: "myProject",
  components: {
    myProjectDetail,
  },
  data() {
    return {
      // 获取项目列表的参数对象
      queryInfo: {
        query: "",
        // 当前的页数
        pagenum: 1,
        // 当前每页显示多少条数据
        pagesize: 5,
      },
      searchInputKey: "",
      projectList: [],

      total: 0,

      // 搜索类型
      searchType: "name",
      searchTypeList: "",
      // 搜索placeholder
      searchTypePlaceholder: "搜索获奖名称",
      searchTypeOptions: [
        {
          value: "name",
          label: "项目名称",
        },
        {
          value: "commander",
          label: "项目作者",
        },
        {
          value: "subject_classification",
          label: "学科分类",
        },
      ],
    };
  },
  methods: {
    // 获取项目列表
    async getProjectList() {
      const { data: res } = await this.$axios.get("/project", {
        params: {
          queryInfo: this.queryInfo,
          searchType: this.searchType,
          username: this.moreUserInfo.username,
        },
      });
      if (res.status !== 200) {
        return this.$message.error("获取项目信息失败");
      }
      // this.$message.success("获取项目信息成功");
      this.projectList = res.result;
      this.total = res.total;
    },
    // 监听 页码值 改变的事件
    handleCurrentChange(newPage) {
      console.log(newPage);
      this.queryInfo.pagenum = newPage;
      this.getProjectList();
    },
    // 监听 pagesize 改变的事件
    handleSizeChange(newSize) {
      // console.log(newSize)
      this.queryInfo.pagesize = newSize;
      this.getProjectList();
    },
    // 搜索功能
    async projectSearchInputChange() {
      // console.log(this.queryInfo);
      this.queryInfo.query = this.searchInputKey;
      this.getProjectList();
    },
    handleProjectDetailShow(data) {
      this.$bus.$emit("handleProjectDetailShow", data);
    },
    // 搜索类型
    handleSearchSelectInputChange(val) {
      try {
        this.searchTypeOptions.forEach((item) => {
          if (item.value == val) {
            this.searchTypePlaceholder = `搜索${item.label}`;
            this.searchType = val;
            console.log(this.searchType);
            throw new Error("");
          }
        });
      } catch (err) {}
    },
  },
  computed: {
    ...mapState(["subject_classificationList", "collegeList", "moreUserInfo"]),
  },
  mounted() {
    this.getProjectList();
    this.$bus.$on("handleGetProjectList", this.getProjectList);
  },
  beforeDestroy() {
    this.$bus.$off("handleGetProjectList");
  },
};
</script>

<style lang="less" scoped>
.searchBox {
  display: flex;
  width: 100%;
}

.status {
  span {
    margin-left: 20px;
  }
}

.projectName {
  cursor: pointer;
  background-color: rgba(185, 184, 184, 0.1);
  border-radius: 20px;
  text-align: center;
  font-weight: 700;
}

// 项目列表
.project-title {
  width: 100%;
  text-align: center;
  margin-bottom: 20px;
}

.mask {
  overflow: hidden;
  height: 0vh;
  animation: maskAni 1s forwards;
}

// 内容
.mask.content {
  display: flex;
  flex-direction: column;
  flex-wrap: wrap;
  justify-content: space-around;
}

@keyframes maskAni {
  100% {
    height: 82vh;
  }
}
</style>